package NetworkConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class NetworkConfiguration_1_SetExampleMessage extends Composite {
	private Text text;
	private Text text_1;
	private Text text_2;
	private Text text_3;
	private Text text_4;
	private Text text_5;
	private Text text_6;
	private Table table;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public NetworkConfiguration_1_SetExampleMessage(Composite parent, int style) {
		super(parent, style);
		setSize(1000,800);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		

		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		lblNewLabel.setBounds(93, 10, 227, 23);
		lblNewLabel.setText("网络配置(网络分配)");
		lblNewLabel.setFont(new Font(getDisplay(), "宋体",18,SWT.RIGHT));
		
		Label label = new Label(this, SWT.NONE);
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setText("基本参数设置：");
		label.setBounds(129, 39, 132, 23);
		
		Label label_1 = new Label(this, SWT.NONE);
		label_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_1.setText("设备数：");
		label_1.setBounds(187, 68, 48, 23);
		
		Label label_2 = new Label(this, SWT.NONE);
		label_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_2.setText("交换机数：");
		label_2.setBounds(423, 68, 61, 23);
		
		Label label_3 = new Label(this, SWT.NONE);
		label_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_3.setText("链路数：");
		label_3.setBounds(187, 97, 48, 23);
		
		Label label_4 = new Label(this, SWT.NONE);
		label_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_4.setText("任务数：");
		label_4.setBounds(433, 97, 51, 23);

		
		text = new Text(this, SWT.BORDER);
		text.setBounds(251, 65, 102, 23);
		
		text_1 = new Text(this, SWT.BORDER);
		text_1.setBounds(505, 65, 102, 23);
		
		text_2 = new Text(this, SWT.BORDER);
		text_2.setBounds(251, 94, 102, 23);
		
		text_3 = new Text(this, SWT.BORDER);
		text_3.setBounds(505, 97, 102, 23);
		

		
		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnNewButton.setBounds(284, 168, 80, 27);
		btnNewButton.setText("清空");
		
		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.setBounds(418, 168, 80, 27);
		btnNewButton_1.setText("保存");
		
		Label label_7 = new Label(this, SWT.NONE);
		label_7.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_7.setText("信号设置：");
		label_7.setBounds(129, 218, 66, 23);
		// table的初始化
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(129, 247, 478, 147);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableItem tableItem_1 = new TableItem(table, SWT.NONE);
		tableItem_1.setText("#1");
				
		TableItem tableItem_2 = new TableItem(table, SWT.NONE);
		tableItem_2.setText("#2");
				
		TableItem tableItem_3 = new TableItem(table, SWT.NONE);
		tableItem_3.setText("#3");
		
		TableItem tableItem_4= new TableItem(table, SWT.NONE);
		tableItem_4.setText("#4");
				
		TableItem tableItem_5 = new TableItem(table, SWT.NONE);
		tableItem_5.setText(new String[] {});
		tableItem_5.setText("#5");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.CENTER);
		tableColumn_1.setWidth(160);
		tableColumn_1.setText("信号");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.CENTER);
		tableColumn_2.setWidth(160);
		tableColumn_2.setText("信号源设备");
		
		TableColumn tableColumn_3 = new TableColumn(table, SWT.CENTER);
		tableColumn_3.setWidth(124);
		tableColumn_3.setText("信号目标设备");
		
		table.addListener(SWT.MouseDoubleClick, listener);
		
		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		button.setText("返回");
		button.setBounds(284, 412, 80, 27);
		
		Button button_1 = new Button(this, SWT.NONE);
		button_1.setText("确定");
		button_1.setBounds(418, 412, 80, 27);
		
		Label label_5 = new Label(this, SWT.NONE);
		label_5.setText("信号数：");
		label_5.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_5.setBounds(187, 126, 48, 23);
		
		text_6 = new Text(this, SWT.BORDER);
		text_6.setBounds(251, 123, 102, 23);
	}
	
	
	//为每个TableItem增加Listener， 当有Selection事件时， 根据行和列可以定位到每一个Cell，然后就可以处理事件，
		//为一个Cell添加可编辑的方法是增加一个TableEditor
		private Listener listener = new Listener() {
			private int EDITABLECOLUMN;

			public void handleEvent(Event event) {
				//获取鼠标点击的位置
				Point point = new Point(event.x, event.y);
			
				//得到鼠标点击的行（item）
				TableItem item = table.getItem(point);
				if (item == null) {
					return;
				}
				for (int i = 1; i < 6; i++) {
					
					//得到选中行（item）的每一列的矩形范围，根据鼠标点击位置point来进一步确定是哪一格（cell）
					Rectangle rect = item.getBounds(i);
					if (rect.contains(point)) {
						EDITABLECOLUMN = i;
						
						final TableEditor editor = new TableEditor(table);
						Control oldEditor = editor.getEditor();
						if (oldEditor != null)
						{
							oldEditor.dispose();
						}
						
						// 添加Text，并使高度符合item的高度
						final Text text = new Text(table, SWT.NONE);
						text.computeSize(SWT.DEFAULT,table.getItemHeight());

						//调整editor的高度宽度
						editor.grabHorizontal = true;
						editor.minimumHeight = text.getSize().y;
						editor.minimumWidth = text.getSize().x;
						
						editor.setEditor(text, item, EDITABLECOLUMN);
						
						text.setText(item.getText(EDITABLECOLUMN));

						text.forceFocus();
						
						// 开始编辑的事件
						text.addModifyListener(new ModifyListener() {
							public void modifyText(ModifyEvent e) {
								text.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLUE));
								editor.getItem().setText(EDITABLECOLUMN,text.getText());
							}
						});
						
						//编辑后内容恢复表格前景色
						text.addFocusListener(new FocusAdapter() {
							public void focusLost(FocusEvent e) {
								text.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
							}
						});
					}
				}
			}
		};

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
